﻿#if !NETFRAMEWORK
using SkiaSharp;
#else
using System.Drawing;
using System.Drawing.Imaging;
#endif
using System.IO;
using ZXing;
using ZXing.Common;

namespace WebSelfHost.Utility.Files
{
    public class BarCodeUtils
    {
#if !NETFRAMEWORK
        public static SKImage EncodeBarCodeSKImage(BarcodeFormat barcodeFormat, string message, int widthPixels, int hightPixels)
        {
            BitMatrix matrix = new MultiFormatWriter().encode(message, barcodeFormat, widthPixels, hightPixels);
            var info = new SKImageInfo(widthPixels, hightPixels);
            using (var surface = SKSurface.Create(info))
            {
                var canvas = surface.Canvas;
                for (int x = 0; x < matrix.Width; x++)
                {
                    for (int y = 0; y < matrix.Height; y++)
                    {

                        if (matrix[x, y])
                        {
                            canvas.DrawRect(x, y, 1, 1, new SKPaint
                            {
                                Color = SKColors.Black,
                                Style = SKPaintStyle.Fill
                            });
                        }
                    }
                }
                return surface.Snapshot();
            }
        }
        public static SKImage EncodeBarCodeSKImage(string message, int widthPixels, int hightPixels)
        {
            return EncodeBarCodeSKImage(BarcodeFormat.CODE_128, message, widthPixels, hightPixels);
        }

        public static SKImage EncodeBarCodeSKImage(string message)
        {
            return EncodeBarCodeSKImage(BarcodeFormat.CODE_128, message, 960, 500);
        }

        public static SKData EncodeBarCodeSKData(BarcodeFormat barcodeFormat, string message, int widthPixels, int hightPixels)
        {
            using (var image = EncodeBarCodeSKImage(barcodeFormat, message, widthPixels, hightPixels))
            {
                return image.Encode(SKEncodedImageFormat.Png, 100);
            }
        }

        public static SKData EncodeBarCodeSKData(string message, int widthPixels, int hightPixels)
        {
            return EncodeBarCodeSKData(BarcodeFormat.CODE_128, message, widthPixels, hightPixels);
        }


        public static SKData EncodeBarCodeSKData(string message)
        {
            return EncodeBarCodeSKData(BarcodeFormat.CODE_128, message, 960, 500);
        }
#else
        public static Image EncodeBarCodeImage(BarcodeFormat barcodeFormat, string message, int widthPixels, int hightPixels)
        {
            BitMatrix matrix = new MultiFormatWriter().encode(message, barcodeFormat, widthPixels, hightPixels);
            Bitmap bmap = new Bitmap(matrix.Width, matrix.Height, PixelFormat.Format32bppArgb);
            for (int x = 0; x < matrix.Width; x++)
            {
                for (int y = 0; y < matrix.Height; y++)
                {
                    if (matrix[x, y])
                        bmap.SetPixel(x, y, Color.Black);
                }
            }
            return bmap;
        }
        public static Image EncodeBarCodeImage(string message, int widthPixels, int hightPixels)
        {
            return EncodeBarCodeImage(BarcodeFormat.CODE_128, message, widthPixels, hightPixels);
        }

        public static Image EncodeBarCodeImage(string message)
        {
            return EncodeBarCodeImage(BarcodeFormat.CODE_128, message, 960, 500);
        }

#endif



        public static byte[] EncodeBarCodeBytes(BarcodeFormat barcodeFormat, string message, int widthPixels, int hightPixels)
        {
#if !NETFRAMEWORK
            using (var data = EncodeBarCodeSKData(barcodeFormat, message, widthPixels, hightPixels))
            {
                return data.ToArray();
            }
#else
            using (MemoryStream mostream = new MemoryStream())
            {
                using (var data = EncodeBarCodeImage(barcodeFormat, message, widthPixels, hightPixels))
                {
                    data.Save(mostream, ImageFormat.Png);
                    byte[] bt = new byte[mostream.Length];
                    mostream.Position = 0;//设置留的初始位置
                    mostream.Read(bt, 0, bt.Length);
                    return bt;
                }
            }
#endif
        }


        public static byte[] EncodeBarCodeBytes(string message, int widthPixels, int hightPixels)
        {
            return EncodeBarCodeBytes(BarcodeFormat.CODE_128, message, widthPixels, hightPixels);
        }


        public static byte[] EncodeBarCodeBytes(string message)
        {
            return EncodeBarCodeBytes(BarcodeFormat.CODE_128, message, 960, 500);
        }
    }
}
